<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
</head>
<body>
    <!--导航栏-->
    <div class="nav">
        <img src="pic/小组头像.jpg">
        <span>我的博客系统</span>
        <!-- 空白元素，用来占位 -->
        <div class="spacer"></div>

        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="logout">注销</a>
    </div>
    <div class="container">
        <div class="left">
            <!--用户信息区-->
            <div class="card">
                <img src="pic/小组头像.jpg">
                <h3></h3>
                <a href="#">github地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>点赞</span>
                </div>
                <div class="counter">
                    <span id="articleNum"></span>
                    <span id="likeNum"></span>
                </div>
            </div>
        </div>
        <div class="right">
            <!-- <div class="blog">
                <div class="title">
                    我的第一篇博客
                </div>
                <div class="date">
                    2022-05-06 13:44:00
                </div>
                <div class="desc">
                    哈哈哈
                </div>
                <a href="blog_detail.html">查看全文&gt;&gt;</a>
            </div> -->
        </div>
    </div>

    <script>
        function getBlogList(){
            $.ajax({
                type: 'get',
                url: 'blog',
                success: function(body){//这里参数要带一个body
                    let rightDiv = document.querySelector('.right');
                    for (let blog of body){//依次获取JSON对象里的blog对象
                        //创建一个空blog,再放到盒子上
                        let blogDiv = document.createElement('div');
                        blogDiv.className = 'blog';
                        //开始构造博客的具体内容
                        //标题
                        let titleDiv = document.createElement('div');
                        titleDiv.className = 'title';//为了样式对，加一个类
                        titleDiv.innerHTML = blog.title;//写内容(注意这里是blog，不是body)
                        blogDiv.appendChild(titleDiv);//最后加到盒子上就行
                        //时间
                        let timeDiv = document.createElement('div');
                        timeDiv.className = 'date';
                        timeDiv.innerHTML = blog.postTime;
                        blogDiv.appendChild(timeDiv);
                        //正文
                        let contentDiv = document.createElement('div');//注意是创建，不是查找
                        contentDiv.className = 'desc';
                        contentDiv.innerHTML = blog.content;
                        blogDiv.appendChild(contentDiv);
                        //a标签
                        let a = document.createElement('a');
                        a.innerHTML = '查看全文 &gt;&gt;';
                        a.href = "blog_detail.html?blogId=" + blog.blogId;
                        blogDiv.appendChild(a);

                        rightDiv.appendChild(blogDiv);
                    }
                },
                error: function(){
                    alert("获取博客列表失败！");
                }
            })
        }
        getBlogList();

        function getUserInfo(pagename){
            $.ajax({
                type: 'get',
                url: 'login',
                success: function(body){
                    if (body.userId && body.userId > 0){//存在并且大于0
                        console.log("登录成功,用户名为:" + body.username);
                        if (pagename == "blog_list.html"){//博客详情页的名字需要另外改
                            changeUserName(body.username, body.articleNum, body.likeNum);
                        }
                    }else {
                        alert("当前未登录，请登录后访问!");
                        location.assign("blog_login.html");//重定向的方法
                    }
                }
            })
        }
        getUserInfo("blog_list.html");

        function changeUserName(username, articleNum, likeNum){
            let h3 = document.querySelector('.card>h3');
            let article = document.querySelector('.counter>#articleNum');
            let like = document.querySelector('.counter>#likeNum');
            h3.innerHTML = username;
            article.innerHTML = articleNum;
            like.innerHTML = likeNum;
        }
    </script>
</body>
</html>